final class CollisionMap {
	private final int offsetX;
	private final int offsetY;
	private final int anInt292;
	private final int anInt293;
	public final int[][] collisionFlags;

	public void reset() {
		for (int i = 0; i < anInt292; i++) {
			for (int j = 0; j < anInt293; j++) {
				if ((i == 0) || (j == 0) || (i == anInt292 - 1) || (j == anInt293 - 1)) {
					collisionFlags[i][j] = 0xffffff;
				} else {
					collisionFlags[i][j] = 0x1000000;
				}
			}
		}
	}

	public void method211(int type, int rotation, int x, int y, boolean flag) {
		x -= offsetX;
		y -= offsetY;
		if (type == 0) {
			if (rotation == 0) {
				flagMask(x, y, 128);
				flagMask(x - 1, y, 8);
			}
			if (rotation == 1) {
				flagMask(x, y, 2);
				flagMask(x, y + 1, 32);
			}
			if (rotation == 2) {
				flagMask(x, y, 8);
				flagMask(x + 1, y, 128);
			}
			if (rotation == 3) {
				flagMask(x, y, 32);
				flagMask(x, y - 1, 2);
			}
		}
		if ((type == 1) || (type == 3)) {
			if (rotation == 0) {
				flagMask(x, y, 1);
				flagMask(x - 1, y + 1, 16);
			}
			if (rotation == 1) {
				flagMask(x, y, 4);
				flagMask(x + 1, y + 1, 64);
			}
			if (rotation == 2) {
				flagMask(x, y, 16);
				flagMask(x + 1, y - 1, 1);
			}
			if (rotation == 3) {
				flagMask(x, y, 64);
				flagMask(x - 1, y - 1, 4);
			}
		}
		if (type == 2) {
			if (rotation == 0) {
				flagMask(x, y, 130);
				flagMask(x - 1, y, 8);
				flagMask(x, y + 1, 32);
			}
			if (rotation == 1) {
				flagMask(x, y, 10);
				flagMask(x, y + 1, 32);
				flagMask(x + 1, y, 128);
			}
			if (rotation == 2) {
				flagMask(x, y, 40);
				flagMask(x + 1, y, 128);
				flagMask(x, y - 1, 2);
			}
			if (rotation == 3) {
				flagMask(x, y, 160);
				flagMask(x, y - 1, 2);
				flagMask(x - 1, y, 8);
			}
		}
		if (flag) {
			if (type == 0) {
				if (rotation == 0) {
					flagMask(x, y, 0x10000);
					flagMask(x - 1, y, 4096);
				}
				if (rotation == 1) {
					flagMask(x, y, 1024);
					flagMask(x, y + 1, 16384);
				}
				if (rotation == 2) {
					flagMask(x, y, 4096);
					flagMask(x + 1, y, 0x10000);
				}
				if (rotation == 3) {
					flagMask(x, y, 16384);
					flagMask(x, y - 1, 1024);
				}
			}
			if ((type == 1) || (type == 3)) {
				if (rotation == 0) {
					flagMask(x, y, 512);
					flagMask(x - 1, y + 1, 8192);
				}
				if (rotation == 1) {
					flagMask(x, y, 2048);
					flagMask(x + 1, y + 1, 32768);
				}
				if (rotation == 2) {
					flagMask(x, y, 8192);
					flagMask(x + 1, y - 1, 512);
				}
				if (rotation == 3) {
					flagMask(x, y, 32768);
					flagMask(x - 1, y - 1, 2048);
				}
			}
			if (type == 2) {
				if (rotation == 0) {
					flagMask(x, y, 0x10400);
					flagMask(x - 1, y, 4096);
					flagMask(x, y + 1, 16384);
				}
				if (rotation == 1) {
					flagMask(x, y, 5120);
					flagMask(x, y + 1, 16384);
					flagMask(x + 1, y, 0x10000);
				}
				if (rotation == 2) {
					flagMask(x, y, 20480);
					flagMask(x + 1, y, 0x10000);
					flagMask(x, y - 1, 1024);
				}
				if (rotation == 3) {
					flagMask(x, y, 0x14000);
					flagMask(x, y - 1, 1024);
					flagMask(x - 1, y, 4096);
				}
			}
		}
	}

	public void method212(boolean flag, int sizeX, int sizeY, int x, int y, int rotation) {
		int mask = 256;
		if (flag) {
			mask += 0x20000;
		}
		x -= offsetX;
		y -= offsetY;
		if ((rotation == 1) || (rotation == 3)) {
			int l1 = sizeX;
			sizeX = sizeY;
			sizeY = l1;
		}
		for (int offsetX = x; offsetX < x + sizeX; offsetX++) {
			if ((offsetX >= 0) && (offsetX < anInt292)) {
				for (int offsetY = y; offsetY < y + sizeY; offsetY++) {
					if ((offsetY >= 0) && (offsetY < anInt293)) {
						flagMask(offsetX, offsetY, mask);
					}
				}
			}
		}
	}

	public void setMask(int x, int y) {
		x -= offsetX;
		y -= offsetY;
		collisionFlags[x][y] |= 0x200000;
	}

	private void flagMask(int x, int y, int mask) {
		collisionFlags[x][y] |= mask;
	}

	public void method215(int rotation, int type, boolean flag, int x, int y) {
		x -= offsetX;
		y -= offsetY;
		if (type == 0) {
			if (rotation == 0) {
				method217(128, x, y);
				method217(8, x - 1, y);
			}
			if (rotation == 1) {
				method217(2, x, y);
				method217(32, x, y + 1);
			}
			if (rotation == 2) {
				method217(8, x, y);
				method217(128, x + 1, y);
			}
			if (rotation == 3) {
				method217(32, x, y);
				method217(2, x, y - 1);
			}
		}
		if ((type == 1) || (type == 3)) {
			if (rotation == 0) {
				method217(1, x, y);
				method217(16, x - 1, y + 1);
			}
			if (rotation == 1) {
				method217(4, x, y);
				method217(64, x + 1, y + 1);
			}
			if (rotation == 2) {
				method217(16, x, y);
				method217(1, x + 1, y - 1);
			}
			if (rotation == 3) {
				method217(64, x, y);
				method217(4, x - 1, y - 1);
			}
		}
		if (type == 2) {
			if (rotation == 0) {
				method217(130, x, y);
				method217(8, x - 1, y);
				method217(32, x, y + 1);
			}
			if (rotation == 1) {
				method217(10, x, y);
				method217(32, x, y + 1);
				method217(128, x + 1, y);
			}
			if (rotation == 2) {
				method217(40, x, y);
				method217(128, x + 1, y);
				method217(2, x, y - 1);
			}
			if (rotation == 3) {
				method217(160, x, y);
				method217(2, x, y - 1);
				method217(8, x - 1, y);
			}
		}
		if (flag) {
			if (type == 0) {
				if (rotation == 0) {
					method217(0x10000, x, y);
					method217(4096, x - 1, y);
				}
				if (rotation == 1) {
					method217(1024, x, y);
					method217(16384, x, y + 1);
				}
				if (rotation == 2) {
					method217(4096, x, y);
					method217(0x10000, x + 1, y);
				}
				if (rotation == 3) {
					method217(16384, x, y);
					method217(1024, x, y - 1);
				}
			}
			if ((type == 1) || (type == 3)) {
				if (rotation == 0) {
					method217(512, x, y);
					method217(8192, x - 1, y + 1);
				}
				if (rotation == 1) {
					method217(2048, x, y);
					method217(32768, x + 1, y + 1);
				}
				if (rotation == 2) {
					method217(8192, x, y);
					method217(512, x + 1, y - 1);
				}
				if (rotation == 3) {
					method217(32768, x, y);
					method217(2048, x - 1, y - 1);
				}
			}
			if (type == 2) {
				if (rotation == 0) {
					method217(0x10400, x, y);
					method217(4096, x - 1, y);
					method217(16384, x, y + 1);
				}
				if (rotation == 1) {
					method217(5120, x, y);
					method217(16384, x, y + 1);
					method217(0x10000, x + 1, y);
				}
				if (rotation == 2) {
					method217(20480, x, y);
					method217(0x10000, x + 1, y);
					method217(1024, x, y - 1);
				}
				if (rotation == 3) {
					method217(0x14000, x, y);
					method217(1024, x, y - 1);
					method217(4096, x - 1, y);
				}
			}
		}
	}

	public void method216(int rotation, int sizeX, int x, int y, int sizeY, boolean flag) {
		int mask = 256;
		if (flag) {
			mask += 0x20000;
		}
		x -= offsetX;
		y -= offsetY;
		if ((rotation == 1) || (rotation == 3)) {
			// int sizeX_2 = sizeX; Not sure what this does.
			// sizeY = sizeX_2;
			sizeX = sizeY;
			sizeY = sizeX;
		}
		for (int offsetX = x; offsetX < x + sizeX; offsetX++) {
			if ((offsetX >= 0) && (offsetX < anInt292)) {
				for (int offsetY = y; offsetY < y + sizeY; offsetY++) {
					if ((offsetY >= 0) && (offsetY < anInt293)) {
						method217(mask, offsetX, offsetY);
					}
				}
			}
		}
	}

	private void method217(int i, int x, int y) {
		collisionFlags[x][y] &= 0xffffff - i;
	}

	public void method218(int x, int y) {
		x -= offsetX;
		y -= offsetY;
		collisionFlags[x][y] &= 0xdfffff;
	}

	/**
	 * Object type 0-3. Usually this objects of this type are walls (and
	 * sometimes doors, i.e. Neitiznot)
	 * 
	 * @param type
	 *            The object type.
	 * @param rotation
	 *            The object rotation.
	 * @param destX
	 *            The destined x location.
	 * @param srcX
	 *            The start x.
	 * @param destY
	 *            The destined y location.
	 * @param srcY
	 *            The start y.
	 * @return
	 */
	public boolean isWalkableWall(int type, int rotation, int destX, int srcX, int destY, int srcY) {
		if ((srcX == destX) && (srcY == destY)) {
			return true;
		}
		srcX -= offsetX;
		srcY -= offsetY;
		destX -= offsetX;
		destY -= offsetY;
		if (type == 0) {
			if (rotation == 0) {
				if ((srcX == destX - 1) && (srcY == destY)) {
					return true;
				}
				if ((srcX == destX) && (srcY == destY + 1)
				&& ((collisionFlags[srcX][srcY] & 0x1280120) == 0)) {
					return true;
				}
				if ((srcX == destX) && (srcY == destY - 1)
				&& ((collisionFlags[srcX][srcY] & 0x1280102) == 0)) {
					return true;
				}
			} else if (rotation == 1) {
				if ((srcX == destX) && (srcY == destY + 1)) {
					return true;
				}
				if ((srcX == destX - 1) && (srcY == destY)
				&& ((collisionFlags[srcX][srcY] & 0x1280108) == 0)) {
					return true;
				}
				if ((srcX == destX + 1) && (srcY == destY)
				&& ((collisionFlags[srcX][srcY] & 0x1280180) == 0)) {
					return true;
				}
			} else if (rotation == 2) {
				if ((srcX == destX + 1) && (srcY == destY)) {
					return true;
				}
				if ((srcX == destX) && (srcY == destY + 1)
				&& ((collisionFlags[srcX][srcY] & 0x1280120) == 0)) {
					return true;
				}
				if ((srcX == destX) && (srcY == destY - 1)
				&& ((collisionFlags[srcX][srcY] & 0x1280102) == 0)) {
					return true;
				}
			} else if (rotation == 3) {
				if ((srcX == destX) && (srcY == destY - 1)) {
					return true;
				}
				if ((srcX == destX - 1) && (srcY == destY)
				&& ((collisionFlags[srcX][srcY] & 0x1280108) == 0)) {
					return true;
				}
				if ((srcX == destX + 1) && (srcY == destY)
				&& ((collisionFlags[srcX][srcY] & 0x1280180) == 0)) {
					return true;
				}
			}
		}
		if (type == 2) {
			if (rotation == 0) {
				if ((srcX == destX - 1) && (srcY == destY)) {
					return true;
				}
				if ((srcX == destX) && (srcY == destY + 1)) {
					return true;
				}
				if ((srcX == destX + 1) && (srcY == destY)
				&& ((collisionFlags[srcX][srcY] & 0x1280180) == 0)) {
					return true;
				}
				if ((srcX == destX) && (srcY == destY - 1)
				&& ((collisionFlags[srcX][srcY] & 0x1280102) == 0)) {
					return true;
				}
			} else if (rotation == 1) {
				if ((srcX == destX - 1) && (srcY == destY)
				&& ((collisionFlags[srcX][srcY] & 0x1280108) == 0)) {
					return true;
				}
				if ((srcX == destX) && (srcY == destY + 1)) {
					return true;
				}
				if ((srcX == destX + 1) && (srcY == destY)) {
					return true;
				}
				if ((srcX == destX) && (srcY == destY - 1)
				&& ((collisionFlags[srcX][srcY] & 0x1280102) == 0)) {
					return true;
				}
			} else if (rotation == 2) {
				if ((srcX == destX - 1) && (srcY == destY)
				&& ((collisionFlags[srcX][srcY] & 0x1280108) == 0)) {
					return true;
				}
				if ((srcX == destX) && (srcY == destY + 1)
				&& ((collisionFlags[srcX][srcY] & 0x1280120) == 0)) {
					return true;
				}
				if ((srcX == destX + 1) && (srcY == destY)) {
					return true;
				}
				if ((srcX == destX) && (srcY == destY - 1)) {
					return true;
				}
			} else if (rotation == 3) {
				if ((srcX == destX - 1) && (srcY == destY)) {
					return true;
				}
				if ((srcX == destX) && (srcY == destY + 1)
				&& ((collisionFlags[srcX][srcY] & 0x1280120) == 0)) {
					return true;
				}
				if ((srcX == destX + 1) && (srcY == destY)
				&& ((collisionFlags[srcX][srcY] & 0x1280180) == 0)) {
					return true;
				}
				if ((srcX == destX) && (srcY == destY - 1)) {
					return true;
				}
			}
		}
		/**
		 * Handles wall decoration clipping flags.
		 */
		if (type == 9) {
			if ((srcX == destX) && (srcY == destY + 1)
			&& ((collisionFlags[srcX][srcY] & 0x20) == 0)) {
				return true;
			}
			if ((srcX == destX) && (srcY == destY - 1) && ((collisionFlags[srcX][srcY] & 2) == 0)) {
				return true;
			}
			if ((srcX == destX - 1) && (srcY == destY) && ((collisionFlags[srcX][srcY] & 8) == 0)) {
				return true;
			}
			if ((srcX == destX + 1) && (srcY == destY)
			&& ((collisionFlags[srcX][srcY] & 0x80) == 0)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Object types 6-8 represent diagonal walls (and sometimes fence doors)
	 * 
	 * @param type
	 *            The object type.
	 * @param rotation
	 *            The object rotation.
	 * @param destX
	 *            The destined x position.
	 * @param srcX
	 *            The start x.
	 * @param destY
	 *            The destined y position.
	 * @param srcY
	 *            The start y.
	 * @return
	 */
	public boolean isWalkableDiagonalWall(int type, int rotation, int destX, int srcX, int destY,
	int srcY) {
		if ((srcX == destX) && (srcY == destY)) {
			return true;
		}
		srcX -= offsetX;
		srcY -= offsetY;
		destX -= offsetX;
		destY -= offsetY;
		if ((type == 6) || (type == 7)) {
			if (type == 7) {
				rotation = rotation + 2 & 3;
			}
			if (rotation == 0) {
				if ((srcX == destX + 1) && (srcY == destY)
				&& ((collisionFlags[srcX][srcY] & 0x80) == 0)) {
					return true;
				}
				if ((srcX == destX) && (srcY == destY - 1)
				&& ((collisionFlags[srcX][srcY] & 2) == 0)) {
					return true;
				}
			} else if (rotation == 1) {
				if ((srcX == destX - 1) && (srcY == destY)
				&& ((collisionFlags[srcX][srcY] & 8) == 0)) {
					return true;
				}
				if ((srcX == destX) && (srcY == destY - 1)
				&& ((collisionFlags[srcX][srcY] & 2) == 0)) {
					return true;
				}
			} else if (rotation == 2) {
				if ((srcX == destX - 1) && (srcY == destY)
				&& ((collisionFlags[srcX][srcY] & 8) == 0)) {
					return true;
				}
				if ((srcX == destX) && (srcY == destY + 1)
				&& ((collisionFlags[srcX][srcY] & 0x20) == 0)) {
					return true;
				}
			} else if (rotation == 3) {
				if ((srcX == destX + 1) && (srcY == destY)
				&& ((collisionFlags[srcX][srcY] & 0x80) == 0)) {
					return true;
				}
				if ((srcX == destX) && (srcY == destY + 1)
				&& ((collisionFlags[srcX][srcY] & 0x20) == 0)) {
					return true;
				}
			}
		}
		if (type == 8) {
			if ((srcX == destX) && (srcY == destY + 1)
			&& ((collisionFlags[srcX][srcY] & 0x20) == 0)) {
				return true;
			}
			if ((srcX == destX) && (srcY == destY - 1) && ((collisionFlags[srcX][srcY] & 2) == 0)) {
				return true;
			}
			if ((srcX == destX - 1) && (srcY == destY) && ((collisionFlags[srcX][srcY] & 8) == 0)) {
				return true;
			}
			if ((srcX == destX + 1) && (srcY == destY)
			&& ((collisionFlags[srcX][srcY] & 0x80) == 0)) {
				return true;
			}
		}
		return false;
	}

	public boolean method221(int destX, int srcX, int destY, int srcY, int l, int i1, int j1) {
		int l1 = (destX + j1) - 1;
		int i2 = (destY + l) - 1;
		if ((srcX >= destX) && (srcX <= l1) && (srcY >= destY) && (srcY <= i2)) {
			return true;
		}
		if ((srcX == destX - 1) && (srcY >= destY) && (srcY <= i2)
		&& ((collisionFlags[srcX - offsetX][srcY - offsetY] & 8) == 0) && ((i1 & 8) == 0)) {
			return true;
		}
		if ((srcX == l1 + 1) && (srcY >= destY) && (srcY <= i2)
		&& ((collisionFlags[srcX - offsetX][srcY - offsetY] & 0x80) == 0) && ((i1 & 2) == 0)) {
			return true;
		}
		return ((srcY == destY - 1) && (srcX >= destX) && (srcX <= l1)
		&& ((collisionFlags[srcX - offsetX][srcY - offsetY] & 2) == 0) && ((i1 & 4) == 0))
		|| ((srcY == i2 + 1) && (srcX >= destX) && (srcX <= l1)
		&& ((collisionFlags[srcX - offsetX][srcY - offsetY] & 0x20) == 0) && ((i1 & 1) == 0));
	}

	public CollisionMap() {
		offsetX = 0;
		offsetY = 0;
		anInt292 = 104;
		anInt293 = 104;
		collisionFlags = new int[anInt292][anInt293];
		reset();
	}
}
